﻿using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Engine;

namespace Game
{
    public class QuickMenu
    {
        Texture2D mTemplateTexture;    
        float     mScale = 18.0f/128.0f;

        List<Pair<Item, short>> mQuickMenu;

        public Rectangle    LeftBorder  = new Rectangle(1, 1, 3, 28);
        public Rectangle    Slot        = new Rectangle(5, 1, 26, 28);
        public Rectangle    RightBorder = new Rectangle(32, 1, 3, 28);

        public Rectangle    Blank       = new Rectangle(000,384,128,128);
        public int2         ActualSlot  = new int2(4, 5);

        Vector2             mPosition;     

        public QuickMenu(int slots, Vector2 position)
        {
            mQuickMenu  = new List<Pair<Item, short>>();

            mPosition   = position;

            for (short e = 0; e < slots; e++) mQuickMenu.Add(null);
        }

        public void TextureLoader(Texture2D template)
        {
            mTemplateTexture = template;
        }

        public void Add(Pair<Item, short> item, short slot)
        {
            if (slot < mQuickMenu.Count)
                mQuickMenu[slot]=item;
        }

        public Item Use(Inventory inventory, short slot)
        {
            if (slot < mQuickMenu.Count)
            {
                if (mQuickMenu[slot] != null)
                {
                    if (mQuickMenu[slot].Second != 1)
                    {
                        mQuickMenu[slot].Second--;
                        return mQuickMenu[slot].First;
                    }
                    else
                    {
                        Item AUX = mQuickMenu[slot].First;
                        inventory.ReLocate(inventory.FindSlot(mQuickMenu[slot].First.Name));
                        inventory.GetInventory().Remove(mQuickMenu[slot]);                        
                        for (short e = 0; e < mQuickMenu.Count; e++)
                            if (mQuickMenu[e] != null)
                                if (mQuickMenu[e].First == AUX)
                                    mQuickMenu[e] = null;
                        return AUX;
                    }
                }
            }
            return null;
        }

        public void UpDate(Inventory inventory)
        {
            for (short e = 0; e < mQuickMenu.Count; e++)
                if (!(inventory.GetInventory().Contains(mQuickMenu[e])))
                    mQuickMenu[e] = null;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Color color = Color.White;
            Vector2 menuposition = mPosition;

            spriteBatch.Draw(mTemplateTexture, 
                             menuposition, 
                             LeftBorder, 
                             color);

            menuposition.X += LeftBorder.Width;

            for (int e = 0; e < mQuickMenu.Count; e++)
            {
                spriteBatch.Draw(mTemplateTexture,
                                 menuposition,
                                 Slot,
                                 color);

                menuposition.X += Slot.Width;
            }

            spriteBatch.Draw(mTemplateTexture, 
                             menuposition,
                             RightBorder, 
                             color);

            for (int e = 0; e < mQuickMenu.Count; e++)
            {
                Vector2 position = mPosition;
                position.X += ActualSlot.X;
                position.X += LeftBorder.Width;
                position.X += Slot.Width * e;
                position.Y += ActualSlot.Y; 

                if (mQuickMenu[e] != null)
                    spriteBatch.Draw(   ItemList.mItemTexture,          //texture
                                        position,                       //position
                                        mQuickMenu[e].First.Rectangle,  //rectangle
                                        color,                          //color
                                        0,                              //angle
                                        Vector2.Zero,                   //origin
                                        mScale,                         //scale
                                        SpriteEffects.None,             //???
                                        0);                             //???
                else
                    spriteBatch.Draw(   ItemList.mItemTexture,  //texture
                                        position,               //position
                                        Blank,                  //rectangle
                                        color,                  //color
                                        0,                      //angle
                                        Vector2.Zero,           //origin
                                        mScale,                 //scale
                                        SpriteEffects.None,     //???
                                        0);                     //???
            }
        }
    } 
}